# Contributor: Raed Rizqie <raed.rizqie@gmail.com>

_realname=gn
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=0.2073.70d6c608
pkgrel=3
pkgdesc='Meta-build system that generates build files for Ninja (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://gn.googlesource.com/gn/"
license=('spdx:BSD')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python"
             "git")
_commit=70d6c60823c0233a0f35eccc25b2b640d2980bdc
source=(git+https://gn.googlesource.com/gn#commit=$_commit
        001-build-fixes.patch
        002-add-missing-header.patch
        003-remove-msvc-flags.patch
        004-add-arm64-arch.patch)
sha256sums=('SKIP'
            'ad74a5b70eae85a380e040f41c529b3684625094dbf7367d997a15294a6355dd'
            'd41ea264f389a34b3030ab5fb39107475a0e913123802dee241a20e1e5ecb1a5'
            '5aec933502008cbe9baabcf57e7899e8153e29c1dea646a52ac71b8eb9306801'
            'f2b66b481336de86b237b5d6062784323e6f359313a8063a9d670d9356e742d6')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

pkgver() {
  cd ${_realname}
  echo 0.$(git rev-list --count initial-commit..).$(git rev-parse --short=8 HEAD)
}

prepare() {
  cd ${_realname}
  apply_patch_with_msg \
  001-build-fixes.patch \
  002-add-missing-header.patch \
  003-remove-msvc-flags.patch \
  004-add-arm64-arch.patch
}

build() {
  cd ${_realname}
  ./build/gen.py --allow-warnings --no-static-libstdc++

  ${MINGW_PREFIX}/bin/ninja -C ./out
}

package() {
  cd ${_realname}
  install -D out/gn "${pkgdir}${MINGW_PREFIX}/bin/gn"
  install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}" docs/*
  install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}" LICENSE

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/vim/vimfiles"
  cp -r misc/vim/{autoload,ftdetect,ftplugin,syntax} \
    "${pkgdir}${MINGW_PREFIX}/share/vim/vimfiles/"
  install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/emacs/site-lisp" misc/emacs/gn-mode.el
}

# vim:set ts=2 sw=2 et:
